package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.business.IPermissionsService;
import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;
import com.younion.vo.PermissionsVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author Joe Chen
*/
public class PermissionsAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(PermissionsAction.class);

	private IPermissionsService permissionsService = null;

	private PermissionsVO permissionsVO = new PermissionsVO();

	private List<PermissionsVO> permissionsList;// 结果集列表
	/**
	 * 列表：翻页区；数据区
	 */
	@RequiresPermissions("listPermissions")
	@SuppressWarnings("unchecked")
	public String listPermissions() {
		try {
			Map paramMap = null;
			if (permissionsVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(permissionsVO));
			}
			// 获取总记录数
			int total = permissionsService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			permissionsList = permissionsService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
	

	/**
	 * 新增
	 */
	@RequiresPermissions("addPermissions")
	public String addPermissions() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	@RequiresPermissions("editPermissions")
	public String editPermissions() {
		try {
			permissionsVO = permissionsService.getById(permissionsVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	@RequiresPermissions("savePermissions")
	public String savePermissions() {
		try {
			permissionsVO.setCode(permissionsVO.getCode().trim());
			permissionsVO = permissionsService.save(permissionsVO);
			String id = permissionsVO.getId() == null ? "" : permissionsVO.getId().toString();
			String log = "".equals(id) ? "添加权限" : "更新权限";
			Integer type = "".equals(id) ? 3 : 4;
			this.addLog(type, log+id, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deletePermissions")
	public String deletePermissions() {
		try {
			String pkId = request.getParameter("ids");
			permissionsService.remove(Arrays.asList(pkId.split(",")));
			this.addLog(3, "删除权限："+pkId, "删除权限",getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	@RequiresPermissions("viewPermissions")
	public String viewPermissions() {
		try {
			String pkId = request.getParameter("ids"); 
			permissionsVO = permissionsService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public PermissionsVO getPermissionsVO() {
		return permissionsVO;
	}

	public void setPermissionsVO(PermissionsVO permissionsVO) {
		this.permissionsVO = permissionsVO;
	}

	public void setPermissionsService(IPermissionsService permissionsService) {
		this.permissionsService = permissionsService;
	}

	public List<PermissionsVO> getPermissionsList() {
		return permissionsList;
	}

	public void setPermissionsList(List<PermissionsVO> permissionsList) {
		this.permissionsList = permissionsList;
	}

}